package models.system;

import models.UUIDModel;
import models.admin.AdminUser;
import models.campus.Campus;
import models.constants.DeletedStatus;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.List;

/**
 * Created by hanbin
 * on 2017/1/10.
 * Time : 下午4:39
 */
@Entity
@Table(name = "ss_companys")
public class Company extends UUIDModel {

    /**
     * 公司名称
     */
    @Column(name = "company_name", length = 20)
    public String companyName;

    /**
     * 公司法人
     */
    @Column(name = "corporation", length = 10)
    public String corporation;

    /**
     * 公司地址
     */
    @Column(name = "company_address", length = 50)
    public String companyAddress;

    /**
     * 公司域名
     */
    @Column(name = "company_domain")
    public String companyDomain;

    /**
     * 联系电话
     */
    @Column(name = "phone", length = 15)
    public String phone;

    /**
     * email邮箱
     */
    @Column(name = "email", length = 30)
    public String email;

    /**
     * 邮政编码
     */
    @Column(name = "zip_code", length = 10)
    public String zipCode;

    /**
     * 组织机构代码
     */
    @Column(name = "org_code", length = 10)
    public String orgCode;

    /**
     * 资质证书编号
     */
    @Column(name = "aptitude_code", length = 50)
    public String aptitudeCode;

    /**
     * 备注信息
     */
    @Column(name = "remark", length = 100)
    public String remark;

    /**
     * 创建人Id
     */
    @Column(name = "ct_user_id")
    public Long ctUserId;

    /**
     * 修改人Id
     */
    @Column(name = "up_user_id")
    public Long upUserId;
    /**
     * 查询所有Company
     *
     * @return
     */
    public static List<Company> loadAllCompany(AdminUser loginUser) {
        if(loginUser.organization != null)
            return Company.find("deleted = ? and id = ?", DeletedStatus.UN_DELETED , loginUser.organization.id).fetch();
        return Company.find("deleted = ?", DeletedStatus.UN_DELETED).fetch();
    }

    /**
     * 根据ID查询物业公司
     * @param id
     * @return
     */
    public static Company findByCompanyId(String id) {
        return Company.find(" deleted = ? and id = ? ", DeletedStatus.UN_DELETED, id).first();
    }

    /**
     * 检查物业公司是否绑定到园区
     * @param companyId
     * @return
     */
    public static Boolean checkIsUsed(String companyId){
        return Campus.count("company.id = ? and deleted = ? " , companyId , DeletedStatus.UN_DELETED) > 0 ;
    }

    //查询所有的company
    public static List<Company> loadCompany(){
        return Company.find(" deleted = ?",DeletedStatus.UN_DELETED).fetch();
    }

    public static Company getByDomain(String domain) {
        // TODO: 暂时只找第一个，等域名加入
        return Company.find("companyDomain = ? and deleted = ? ", domain,DeletedStatus.UN_DELETED).first();
    }

}
